package com.xander.myblog.service.impl;

import com.xander.myblog.constant.Constants;
import com.xander.myblog.entity.User;
import com.xander.myblog.enums.ErrorCodeEnum;
import com.xander.myblog.mapper.UserMapper;
import com.xander.myblog.service.UserService;
import com.xander.myblog.util.MD5Utils;
import com.xander.myblog.util.NamePictureUtil;
import com.xander.myblog.util.ShortUUIDUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

import static com.xander.myblog.util.CheckUtil.check;

/**
 * @author Xander
 * @date 2018-11-20
 */
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;

    @Override
    public User login(User user) {
        check(StringUtils.isNoneEmpty(user.getPassword()), ErrorCodeEnum.PARAMETER_EMPTY, "user={}", user);
        User getUser = getUser(user);
        check(null != getUser, ErrorCodeEnum.ACCOUNT_NOTEXIST, "user={}", user);
        check(getUser.getPassword().equals(MD5Utils.encryptPwd(user.getPassword(), Constants.SALT)), ErrorCodeEnum.ACCOUNT_OR_PASSWORD_ERROR, "user={}", user);
        getUser.setPassword(null);
        return getUser;
    }

    @Override
    public void register(User user, String code, String verCode) {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        check(StringUtils.isNotEmpty(verCode) && code.toLowerCase().equals(verCode.toLowerCase()), ErrorCodeEnum.CAPTCHA_ERROR, "code={},verCode={}", code, verCode);
        check(StringUtils.isNotEmpty(user.getUsername()) || StringUtils.isNotEmpty(user.getNickname()) || StringUtils.isNotEmpty(user.getPassword()), ErrorCodeEnum.PARAMETER_EMPTY, "user={}", user);
        check(user.getPassword().length() > 6, ErrorCodeEnum.UNSAFE_PASSWORD, "user={}", user);
        User findUser = new User();
        findUser.setUsername(user.getUsername());
        User tmp = userMapper.getUser(findUser);
        check(tmp == null, ErrorCodeEnum.ACCOUNT_EXIST, "user={}", user);
        user.setPassword(MD5Utils.encryptPwd(user.getPassword(), Constants.SALT));
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddhhmmss");
        String fileName = sdf.format(new Date()) + "_" + ShortUUIDUtil.getShortUUID();
        String path = request.getServletContext().getRealPath("/WEB-INF/classes/static/static/img/avatar");
        try {
            NamePictureUtil.generateImg(user.getNickname(), path, fileName);
        } catch (IOException e) {
            e.printStackTrace();
        }
        user.setAvatar("/static/img/avatar/" + fileName + ".jpg");
        userMapper.insert(user);
    }

    @Override
    public User getUser(User user) {
        check(StringUtils.isNoneEmpty(user.getUsername()), ErrorCodeEnum.PARAMETER_EMPTY, "user={}", user);
        return userMapper.getUser(user);
    }

    @Override
    public User getUserByPermission(int permission) {
        User user = new User();
        user.setPermission(permission);
        return userMapper.getUser(user);
    }

    @Override
    public void update(User user) {
        userMapper.update(user);
    }
}
